package com.cictec.middleware.gps.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.cictec.middleware.CloudConstants;
import com.cictec.middleware.core.entity.Bus;
import com.cictec.middleware.core.entity.Device;
import com.cictec.middleware.gps.service.feign.MiddlewareDcFeignClient;

/**
 * 设备服务
 * 
 * @author ql
 *
 */
@Service
public class DeviceService {

	private static Logger logger = LoggerFactory.getLogger(DeviceService.class);

	@Autowired
	private StringRedisTemplate stringRedisTemplate;


	/**
	 * 【数据库】获取所有车辆信息
	 * 
	 * @return
	 */
	public List<Device> selectListAllDevice() {
		HashOperations<String, String, String> ops = stringRedisTemplate.opsForHash();
		List<String> json = ops.values(CloudConstants.REDIS_CLOUD_DEVICE);
		List<Device> list = new ArrayList<Device>();
		for (String _device : json) {
			list.add(JSON.parseObject(_device, Device.class));
		}
		return list;
	}

	/**
	 * 根据设备uuid查询设备
	 * 
	 * @param devUuid
	 * @return
	 */
	public Device selectDeviceByDevUuid(String devUuid) {
		List<Device> list = selectListAllDevice();
		for (Device device : list) {
			if (device.getDevUuid().equals(devUuid)) {
				return device;
			}
		}
		return null;
	}

	/**
	 * 根据设备编号查询设备
	 * 
	 * @param devCode
	 * @return
	 */
	public Device selectDeviceByDevCode(String devCode) {
		List<Device> list = selectListAllDevice();
		for (Device device : list) {
			if (device.getDevCode().equals(devCode)) {
				return device;
			}
		}
		return null;
	}

	@Autowired
	private BusService busService;
	@Autowired
	private MiddlewareDcFeignClient middlewareDcFeignClient;

	/**
	 * 根据车辆uuid查询设备
	 * 
	 * @param busUuid
	 * @return
	 */
	public Device selectDeviceByBusUuid(String busUuid) {
		Bus bus = busService.selectBusByUuid(busUuid);

		List<Device> list = selectListAllDevice();
		for (Device device : list) {
			if (device.getDevUuid().equals(bus.getBusDevUuid())) {
				return device;
			}
		}
		return null;
	}

	/**
	 * 根据序列号查询设备编号
	 * 
	 * @param serialNumber
	 * @return
	 */
	public Device selectDeviceByRefId(String serialNumber) {
		List<Device> list = selectListAllDevice();
		for (Device device : list) {
			if (device.getDevRefId().equals(serialNumber)) {
				return device;
			}
		}
		return null;

	}

	/**
	 * 更新设备在线状态
	 * 
	 * @param devUuid
	 * @param devOnlineStatus
	 */
	public void updateDeviceOnlineStatusByDevUuid(String devUuid, String devOnlineStatus) {
		logger.debug("【设备管理服务】【更新设备在线状态】devUuid:{}  status:{}", devUuid, devOnlineStatus);
		middlewareDcFeignClient.updateDeviceOnlineStatusByDevUuid(devUuid, devOnlineStatus);
	}


}
